# -*- coding: cp936 -*-
import cookielib
import urllib2
import urllib
import sys 
import re
import requests
import time
import codecs
from bs4 import BeautifulSoup

#Login url
_Zhihu_URL = 'http://www.zhihu.com'
_Captcha_URL_Prefix = _Zhihu_URL + '/captcha.gif?r='
hosturl = 'http://www.zhihu.com/login/email'

#Deal url
url_list = ['http://www.zhihu.com/question/30071084/answer/60490270', 'http://www.zhihu.com/question/30071084/answer/59992701']

#Login info
user = ''
pwd = ''

session = None

def _save_captcha(url):
        global session
        r = session.get (url)
        with open('c:\code.gif', 'wb') as f:
                f.write(r.content)

def get_captcha_url():
        return _Captcha_URL_Prefix + str(int(time.time() * 1000))

def getHtml (url):
    global session
	page = session.get (url)
	return page.content

def get_xsrf (url):
        html = getHtml (url)
        #name="_xsrf" value=""/
        reg = 'name="_xsrf" value="(.*)"'
        xsrf_reg = re.compile (reg)
        xsrf_list = re.findall (xsrf_reg, html)
        return xsrf_list[0]

def log_in ():
        global session
        _save_captcha (get_captcha_url ())
        captcha = raw_input('captcha: ')
        postData = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
                '_xsrf' : get_xsrf ("http://www.zhihu.com"),
                'email' : user,
                'password' : pwd,
                'rememberme' : 'y',
                'captcha': captcha
        }
        res = session.post (hosturl, data = postData)
        print res.json ()['msg']


def init ():
        global session
        session = requests.Session ()
        
def get_answer_id (page):
        reg = r'data-aid="(.*)"'
        answer_list = re.findall (reg, page)
        return answer_list[0]

def get_comment_num (page):
        reg = r'<i class="z-icon-comment"></i>(.*)</a>'
        num = re.findall (reg, page)
        print num[0]
    
def comment_filter (page):
	soup = BeautifulSoup (page, 'html.parser')
	comment_list = soup.find ('div', {'class' : 'zm-comment-list'}, recursive = 'false')
	for comment in comment_list.find_all ('div', {'class' : 'zm-item-comment'}, recursive = 'false'):
		if (comment):
			user = comment.find ('a', {'class' : 'zm-item-link-avatar'}, recursive = 'false')
			user_name = user.get ('title')
			#print type (user_name)
			if (unicode (user_name) == u'葉神月' or unicode (user_name) == u'王启超'):
				context = comment.find ('div', {'class' : 'zm-comment-content'}, recursive = 'false')
				if (context):
					print user_name
					print unicode (context.string)
	
    
def get_result (url):
        page = getHtml (url)
        answer_id = get_answer_id (page)
        #print get_comment_num (page)
        comment = getHtml ('http://www.zhihu.com/node/AnswerCommentBoxV2?params=%7B%22answer_id%22%3A%22' + answer_id + '%22%2C%22load_all%22%3Atrue%7D')
        comment_filter (comment)
        
if __name__  == '__main__':
	init ()
	log_in ()
	for url in url_list:
		get_result (url)

